// 品牌订单列表
import { TARGET_TYPE } from '../../../constants/common';
import { ORDERLIST_TABS, ORDER_TYPE_ENUM, DELIVERY_TYPE_ENUM } from '../../../constants/order';
import { OrderListMixins, ManageTargetExchangeMixins } from '../../../behaviors/page';
import { getBrandOrderList, getBrandAfterSaleOrderList, deliverBrandOrder, prepareBrandOrder,
    pickBrandOrder, dealBrandAfterSaleOrder, brandAgreeWearhouse, brandRefuseWearhouse } from '../../api/order';
import { PACKAGE_ORDER_PAGE } from '../../../constants/route';
const { navTo, showLoading, hideLoading, tipError } = require('../../../common/env');
Page({
    mixins: [OrderListMixins, ManageTargetExchangeMixins],
    data: {
        orderTypesEnum: ORDER_TYPE_ENUM,
        tabsData: ORDERLIST_TABS,
        orderList: [],
        afterSaleList: [],
        showPickModal: false, // 提货码
        showRefuseModal: false, // 拒绝原因
        showExpressModal: false, // 快递信息
        showRefuseWarehouseModal: false, // 拒绝入库原因
        dealOrderInfo: {}, // 处理订单
        searchTerm: '',
        searchValue: '',
        isHaveFilter: true,
    },
    onLoad: async function (options) {
        const { tab, filterType='', filterValue='' } = options;
        await this._handleExchange(TARGET_TYPE.brand, options.brandId);
        this.setData({
            activeIndex: tab || ORDER_TYPE_ENUM.all,
            searchTerm: filterType,
            searchValue: filterValue,
            isHaveFilter: filterType === '',
        }, async () => {
            await this.getOrderListData(true);
        });
    },
    // 获取订单列表
    getOrderListData: async function (isRefresh = false) {
        showLoading('加载中...');
        const { activeIndex, orderList, afterSaleList, pageSize, pageNo, pageTime, searchTerm, searchValue } = this.data;
        let param = { pageSize, pageNo, pageTime };
        if (searchValue) param = { ...param, searchTerm, searchValue };
        if (activeIndex === ORDER_TYPE_ENUM.afterSale) {
            // 售后
            const afterList = await getBrandAfterSaleOrderList(param);
            console.log('BrandOrderList => 获取售后订单数据为：', afterList, isRefresh);
            this.setData({
                afterSaleList: isRefresh ? afterList : [...afterSaleList, ...afterList],
            });
        } else {
            const isAll = activeIndex === ORDER_TYPE_ENUM.all;
            if (isAll) {
                param.isAll = true;
            } else {
                param.orderStatus = ORDERLIST_TABS.filter(item => item.name === activeIndex)[0].value;
            }
            // 正常单
            const list = await getBrandOrderList(param);
            console.log('BrandOrderList => 获取订单数据为：', list, isRefresh);
            this.setData({
                orderList: isRefresh ? list : [...orderList, ...list],
            });
        }
        hideLoading()
    },
    // 订单详情
    invokeDetailAction: function (event) {
        const { serialNumber } = event.detail;
        navTo(PACKAGE_ORDER_PAGE.brandOrderDetail, {
            orderNumber: serialNumber,
        });
    },
    // 售后单详情
    invokeAfterSaleDetailAction: function (event) {
        const { serialNumber } = event.detail;
        navTo(PACKAGE_ORDER_PAGE.brandAfterSaleDetail, {
            orderNumber: serialNumber,
        });
    },
    // 售后
    // 同意售后
    invokeAgreeAction: async function (event) {
        const { serialNumber, amount } = event.detail;
        const res = await dealBrandAfterSaleOrder(serialNumber, {
            isRefuse: false,
            refuseReason: '',
        });
        if (res) {
            await this.refreshListData();
        }
    },
    // 拒绝售后
    invokeRefuseAction: async function (event) {
        this.setData({
            showRefuseModal: true,
            dealOrderInfo: event.detail,
        });
    },
    refuseModalAction: async function (event) {
        const refuseReason = event.detail;
        const { serialNumber } = this.data.dealOrderInfo;
        if (!refuseReason) {
            tipError('拒绝原因不能为空');
            return;
        }
        const param = {
            isRefuse: true,
            refuseReason,
        };
        const res = await dealBrandAfterSaleOrder(serialNumber, param);
        if (res) {
            await this.refreshListData();
        }
    },
    // 同意入库
    invokeAgreeWarehouseAction: async function (event) {
        const { serialNumber } = event.detail;
        const res = await brandAgreeWearhouse(serialNumber);
        if (res) {
            await this.refreshListData();
        }
    },
    // 拒绝入库
    invokeRefuseWarehouseAction: function (event) {
        this.setData({
            showRefuseWarehouseModal: true,
            dealOrderInfo: event.detail,
        });
    },
    refuseWarehouseModalAction: async function (event) {
        const refuseReason = event.detail;
        const { serialNumber } = this.data.dealOrderInfo;
        if (!refuseReason) {
            tipError('拒绝原因不能为空');
            return;
        }
        const param = {
            refuseAfterSaleReason: refuseReason,
        };
        const res = await brandRefuseWearhouse(serialNumber, param);
        if (res) {
            await this.refreshListData();
        }
    },
    // 订单处理
    // 确认(确认发货、确认备货)
    invokeConfirmAction: async function (event) {
        const { deliveryMode, serialNumber } = event.detail;
        const isExpress = deliveryMode === DELIVERY_TYPE_ENUM.express;
        if (isExpress) {
            // 确认发货
            this.setData({
                showExpressModal: true,
                dealOrderInfo: event.detail,
            });
        } else {
            // 确认备货
            const flag = await prepareBrandOrder(serialNumber);
            setTimeout(async () => {
                await this.refreshListData();
            }, flag ? 0 : 2000);
        }
    },
    // 提交快递发货信息
    expressModalAction: async function (event) {
        const { platform, number } = event.detail;
        const { serialNumber } = this.data.dealOrderInfo;
        if (!platform || !number) {
            tipError('快递信息不完整');
            return;
        }
        const param = {
            platform,
            platformOrderNum: number,
        };
        const flag = await deliverBrandOrder(serialNumber, param);
        setTimeout(async () => {
            await this.refreshListData();
        }, flag ? 0 : 2000);
    },
    // 完成订单
    invokeComplateAction: function (event) {
        this.setData({
            showPickModal: true,
            dealOrderInfo: event.detail,
        })
    },
    // 提交确认提货信息
    pickModalAction: async function (event) {
        const code = event.detail;
        if (!code) {
            tipError('请输入提货码');
            return;
        }
        const { serialNumber } = this.data.dealOrderInfo;
        const res = await pickBrandOrder(serialNumber, code);
        if (!res.flag) {
            tipError(res.msg);
            setTimeout(async () => {
                await this.refreshListData();
            }, 2000);
            return;
        };
        await this.refreshListData();
    },
    // 处理查询条件变化
    invokeFilterAction: function (event) {
        const { type, value } = event.detail;
        // 保存筛选条件
        this.setData({
            pageNo: 1,
            searchTerm: type,
            searchValue: value,
        }, async () => await this.getOrderListData(true));
    },
})